<template>
  <div class="admin-profile-container">
    <span v-for="color in colors" :key="color" :class="['layui-badge-dot', `bg-${color}`]"></span>
    <h2 class="admin-profile-title">及时通讯</h2>
    <el-card class="admin-profile-card" shadow="always">
      <h2 class="title">私聊消息区</h2>
      <div class="chat-body" ref="chatBody" :style="{ height: chatHeight + 'px' }">
        <div v-for="(message, index) in messages" :key="index" class="message-item">
          <div :class="['message', message.type]">
            <span class="username">{{ message.username }}</span>
            <span class="content">{{ message.content }}</span>
          </div>
        </div>
      </div>
      <div class="chat-footer">
        <div class="user-label">
          <h5>{{ username }}对</h5>
          <el-select v-model="selectedUser" placeholder="选择用户" class="select">
            <el-option
                v-for="user in users"
                :key="user.value"
                :label="user.label"
                :value="user.value">
            </el-option>
          </el-select>
          <h5>说：</h5>
        </div>
        <el-input
            v-model="newMessage"
            placeholder="请输入消息"
            @keyup.enter="sendMessage"
        ></el-input>
        <el-button type="primary" @click="sendMessage">发送</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "GroupChat",
  data() {
    return {
      colors: ['red', 'orange', 'green', 'cyan', 'blue', 'gray'],
      messages: [],
      newMessage: "",
      username: "用户A",
      chatHeight: 300,
      selectedUser: "",
      users: [
        { label: "大家", value: "all" },
        { label: "用户B", value: "userB" },
        { label: "用户C", value: "userC" },
        { label: "用户D", value: "userD" }
      ]
    };
  },
  methods: {
    sendMessage() {
      if (this.newMessage.trim() === "") {
        this.$message.warning("消息不能为空！");
        return;
      }

      const newMessage = {
        username: this.username,
        content: this.newMessage,
        type: "self", // 自己发送的消息
      };

      this.messages.push(newMessage);

      this.newMessage = "";

      this.$nextTick(() => {
        this.scrollToBottom();
      });
    },
    scrollToBottom() {
      const container = this.$refs.chatBody;
      container.scrollTop = container.scrollHeight;
    },
  },
};
</script>

<style scoped>
h5 {
  font-size: 15px;
  font-weight: bold; /* 加粗字体 */
  color: var(--primary-color); /* 使用主题颜色 */
  margin: 0; /* 移除默认边距 */
}

.user-label {
  display: flex;
  margin-top: 10px;
  align-items: center; /* 垂直居中 */
  justify-content: flex-start; /* 水平起始对齐 */
  margin-bottom: 10px; /* 添加底部边距 */
}

.bg-red {
  background-color: red;
}

.bg-orange {
  background-color: #FFB900;
}

.bg-green {
  background-color: #009688;
}

.bg-cyan {
  background-color: #03A9F4;
}

.bg-blue {
  background-color: #2196F3;
}

.bg-gray {
  background-color: #9E9E9E;
}

.layui-badge-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin: 2px;
}
.layui-badge-dot:hover {
  transform: scale(1.2);
}

.admin-profile-container {
  width: 100%;
  padding: 20px;
  background-color: var(--background-color);
  color: var(--text-color);
}

.admin-profile-title {
  font-size: 1.2em;
  color: var(--primary-color);
  margin-left: 50px;
  margin-top: 20px;
}

.admin-profile-card {
  background-color: #fff;
  border: 1px solid var(--border-color);
  margin: 20px auto;
  max-width: 600px;
  padding: 20px;
}

.title {
  margin-bottom: 20px;
  font-size: 1.1em;
  color: var(--secondary-color);
}

.admin-profile-card .chat-body {
  height: 200px;
  width: 550px;
  border: 1px solid #ccc;
  overflow-y: auto;
  padding: 10px;
  resize: both; /* 允许水平和垂直调整大小 */
  min-height: 150px;
  max-height: 500px;
  max-width: 550px;
  margin-bottom: 10px;
}

.message-item {
  margin-bottom: 10px;
}

.message {
  display: flex;
  align-items: center;
}

.message .username {
  font-weight: bold;
  margin-right: 5px;
}

.message.self {
  justify-content: flex-end;
}

.message.self .username {
  margin-right: 0;
  margin-left: 5px;
}

.chat-footer {
  display: flex;
  align-items: center; /* 垂直居中 */
  padding: 10px;
}

.chat-footer .el-input {
  flex: 1;
  margin-right: 10px;
}

.select{
  width: 130px;
}
</style>